﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Toybox.NicoSharp.Video.UnitTest
{
	using MyPage;

	[TestClass]
	public class Test_MyPage : AbstractTester
	{
		[TestMethod]
		public void Test_Token()
		{
			Token token = new Token();
			token.Update();
			Assert.IsNotNull(token.Value);

			string url2 = @"http://www.nicovideo.jp/my/fav/tag";
			Token token2 = new Token(url2);
			token2.Update();
			Assert.IsNotNull(token2.Value);

			string url3 = @"http://www.nicovideo.jp/my/mylist";
			Token token3 = new Token(url3);
			token3.Update();
			Assert.IsNotNull(token3.Value);
		}

		#region MyListGroup

		// からっぽの状態でテスト

		[TestMethod]
		public void Test_MyListGroup_1()
		{
			MylistGroup group = new MylistGroup();

			//add
			MyListGroupItem item = new MyListGroupItem();
			item.Name = "new";
			//item.IsPublic = true;
			//item.IconId = IconId.Black;
			//item.Description = "test";
			//item.DefaultSort = SortType.Register_Old;
			MylistGroup.AddResult aResult = group.Add(item);
			Assert.AreEqual("ok", aResult.status);

			//list
			MylistGroup.ListResult lResult = group.List();
			Assert.IsTrue(0 <= lResult.mylistgroup.Length);

			//get
			MylistGroup.GetResult gResult = group.Get(lResult.mylistgroup[0].id);
			Assert.IsNotNull(gResult.mylistgroup.id);

			//update
			item.IsPublic = true;
			item.IconId = IconId.Black;
			item.Description = "update";
			item.DefaultSort = SortType.Play_Small;
			MylistGroup.UpdateResult uResult = group.Update(item);
			Assert.AreEqual("ok", uResult.status);

			//delete
			MylistGroup.DeleteResult dResult = group.Delete(item.GroupId);
			Assert.AreEqual("ok", dResult.status);
		}

		[TestMethod]
		public void Test_MyListGroup_2()
		{
			MylistGroup group = new MylistGroup();

			//sort
			MyListGroupItem item1 = new MyListGroupItem();
			item1.Name = "1";
			MyListGroupItem item2 = new MyListGroupItem();
			item2.Name = "2";

			MylistGroup.AddResult aResult;
			aResult = group.Add(item1);
			Assert.AreEqual("ok", aResult.status);
			aResult = group.Add(item2);
			Assert.AreEqual("ok", aResult.status);

			MylistGroup.SortResult sResult = group.Sort(new string[] { item1.GroupId, item2.GroupId });
			Assert.AreEqual("ok", sResult.status);

			//delete
			MylistGroup.DeleteResult dResult;
			dResult = group.Delete(item1.GroupId);
			Assert.AreEqual("ok", dResult.status);
			dResult = group.Delete(item2.GroupId);
			Assert.AreEqual("ok", dResult.status);

		}

		#endregion MyListGroup


		[TestMethod]
		public void Test_MyList()
		{
			#region 準備

			// リストの準備
			MylistGroup group = new MylistGroup();

			MyListGroupItem item1 = new MyListGroupItem();
			item1.Name = "1";
			MyListGroupItem item2 = new MyListGroupItem();
			item2.Name = "2";

			MylistGroup.AddResult aResult;
			aResult = group.Add(item1);
			Assert.AreEqual("ok", aResult.status);
			aResult = group.Add(item2);
			Assert.AreEqual("ok", aResult.status);

			// 動画準備
			Video video = new Video("sm21109783");
			Flv flv = video.GetFlv();
			string id = flv.thread_id;

			Item item = new Item(ItemType.Douga, id);

			#endregion 準備

			// リスト生成
			Mylist list1 = new Mylist(item1.GroupId);
			Mylist list2 = new Mylist(item2.GroupId);

			// add
			item.Description = "add";
			Mylist.AddResult aResult_ = list1.Add(item);
			Assert.AreEqual(aResult_.status, "ok");

			// list
			Mylist.ListResult lResult = list1.List();
			Assert.AreEqual(lResult.status, "ok");

			// update
			item.Description = "update";
			Mylist.UpdateResult uResult = list1.Update(item);
			Assert.AreEqual(uResult.status, "ok");

			// copy
			Mylist.CopyResult cResult = list1.Copy(list2.Id, item);
			Assert.AreEqual(cResult.status, "ok");

			// delete
			Mylist.DelResult dResult = list2.Delete(item);
			Assert.AreEqual(dResult.status, "ok");

			// move
			Mylist.MoveResult mResult = list1.Move(list2.Id, item);
			Assert.AreEqual(mResult.status, "ok");

			// 後始末
			MylistGroup.DeleteResult dResult_;
			dResult_ = group.Delete(item1.GroupId);
			Assert.AreEqual("ok", dResult_.status);
			dResult_ = group.Delete(item2.GroupId);
			Assert.AreEqual("ok", dResult_.status);
		}

		[TestMethod]
		public void Test_DefList()
		{
			#region 準備

			// リストの準備
			MylistGroup group = new MylistGroup();

			MyListGroupItem gitem = new MyListGroupItem();
			gitem.Name = "group";

			MylistGroup.AddResult aResult;
			aResult = group.Add(gitem);
			Assert.AreEqual("ok", aResult.status);

			// 動画準備
			Video video = new Video("sm21109783");
			Flv flv = video.GetFlv();
			string id = flv.thread_id;

			Item item = new Item(ItemType.Douga, id);

			#endregion 準備

			Deflist deflist = new Deflist();
			Mylist list = new Mylist(gitem.GroupId);

			//add
			item.Description = "add";
			Deflist.AddResult aResult_ = deflist.Add(item);
			Assert.AreEqual(aResult.status, "ok");

			//list
			Deflist.ListResult lResult = deflist.List();
			Assert.IsNotNull(lResult);

			//update
			item.Description = "update";
			Deflist.UpdateResult uResult = deflist.Update(item);
			Assert.AreEqual(uResult.status, "ok");

			//copy
			Deflist.CopyResult cResult = deflist.Copy(list.Id, item);
			Assert.AreEqual(cResult.status, "ok");

			//delete
			Deflist.DelResult dResult = deflist.Delete(item);
			Assert.AreEqual(dResult.status, "ok");

			//move
			Deflist.MoveResult mResult = deflist.Move(list.Id, item);
			Assert.AreEqual(cResult.status, "ok");

			// 後始末
			MylistGroup.DeleteResult dResult_;
			dResult_ = group.Delete(gitem.GroupId);
			Assert.AreEqual("ok", dResult.status);
		}


		[TestMethod]
		public void Test_FriendList()
		{
			FriendList list = new FriendList();

			// list
			FriendList.ListResult lResult = list.List();
			Assert.AreEqual(lResult.status, "ok");

			// delete
			string user = "414388";
			FriendList.DelResult dResult = list.Delete(user);
			Assert.AreEqual(lResult.status, "ok");
		}

		[TestMethod]
		public void Test_WatchItem()
		{
			WatchItem wItem = new WatchItem();

			// list
			WatchItem.ListResult lResult = wItem.List();
			Assert.AreEqual(lResult.status, "ok");

			// add
			string user = "414388";
			WatchItem.AddResult aResult = wItem.Add(user);
			Assert.AreEqual(aResult.status, "ok");

			// exist
			WatchItem.ExistResult eResult = wItem.Exist(user);
			Assert.AreEqual(eResult.status, "ok");

			// delete
			WatchItem.DelResult dResult = wItem.Delete(user);
			Assert.AreEqual(dResult.status, "ok");
		}

	}
}